<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/11
 * Time: 15:33
 */

namespace App\Http\Controllers\Web;


use App\Service\MenuService;
use Illuminate\Http\Response;

class MenuController extends BaseController
{
    public function index()
    {
        $params = $this->request->post();
        return MenuService::getList($params,'order','ASC');
    }


    public function getMenuById()
    {
        $menuId = $this->request->post('id',0);
        if(!$menuId){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        return MenuService::getMenuById($menuId);
    }

    public function add()
    {
        $params = $this->request->post();
        if(!isset($params['title']) || empty($params['title'])){
            return Response()->json(['code'=>201,'msg'=>'菜单名称不能为空']);
        }
        $params['parent_id'] = $this->request->post('parent_id',0);
        $params['order'] = $this->request->post('order',0);
        return MenuService::add($params);


    }
    public function edit()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(intval($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        if(!isset($params['title']) || empty(trim($params['title']))){
            return Response()->json(['code'=>201,'msg'=>'菜单名称不能为空']);
        }
        $params['parent_id'] = $this->request->post('parent_id',0);
        $params['order'] = $this->request->post('order',0);
        return MenuService::edit($params);
    }

    public function destroy()
    {
        $ids = $this->request->post('ids');
        $ids = is_array($ids) ? $ids : [$ids];
        if(!$ids){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的菜单']);
        }
        return MenuService::destroy($ids);
    }


}
